package com.zdkj.subway.stm.util;/**
 * @author 陈博
 * @date 2020/2/7 0007 21:31
 */

import com.zdkj.common.core.exceltools.ExcelUtil;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @program: zdkj
 *
 * @description:导入导出重复代码抽取
 *
 * @author: chen
 *
 * @create: 2020-02-07 21:31
 **/
@Slf4j
@Component
public class ExcelUtilsStm {
    public static Sheet inport(MultipartFile multipartFile){
        Workbook workbook = null;
        try {
            File file = MultipartFileToFile.multipartFileToFile(multipartFile);
            workbook = ExcelUtil.chooseWorkbook(file);
            /**
             * 删除缓存文件
             */
            MultipartFileToFile.delteTempFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Sheet sheet = workbook.getSheetAt(0);

        return sheet;
    }
	  /**
     * 设置居中样式
     * @param workbook
     * @return
     */
    public static CellStyle createCenteredCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        return style;
    }

    /**
     * 设置边框
     * @param workbook
     * @return
     */
    public static CellStyle createBorderCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        return style;
    }

    /**
     * 设置表头样式
     * @param workbook
     * @return
     */
    public static CellStyle createWrappedHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setWrapText(true);
        return style;
    }

    /**
     * 设置列宽度
     * @param sheet
     */
    public static void seettingColumnWidth(Sheet sheet) {
        // 设置从第一列（索引0）开始的所有列的宽度
        int startColumnIndex = 0; // 第一列的索引
        int columnWidth = 6000; // 列宽，单位是字符宽度
        // 获取工作表的最大列数（默认情况下为0）
        int maxColumnIndex = 0;
        for (Row row : sheet) {
            if (row.getLastCellNum() > maxColumnIndex) {
                maxColumnIndex = row.getLastCellNum();
            }
        }
        // 如果没有行，设置一个较大的列数以确保覆盖所有可能的列
        if (maxColumnIndex == 0) {
            maxColumnIndex = 50; // 假设最多有50列
        }
        // 设置从第三列开始的所有列的宽度
        for (int i = startColumnIndex; i <= maxColumnIndex; i++) {
            sheet.setColumnWidth(i, columnWidth);
        }
    }
}
